//地址搜索页
const app = getApp()
var server = require('../../utils/server');
var common = require('../../utils/common');
var amapFile = require('../../libs/amap-wx.js');
var hideTimer = null;
Page({
  data: {
    selectedAddr: "",
    nearbyBuildings: [],
    city: "",
    isDeliverable: true,
    searchValue: "",
    curCity: "定位中",
    showNearBy: true,
    curPname: "",
    curCityname: "",
    curAdname: "",
    curlon: "",
    curla: "",
    fromType: "",
    curCityid: ""
  },
  onLoad(options = {}) {
    this.data.fromType = options.fromType == undefined ? '' : options.fromType;
  },
  onShow: function() {
    wx.getStorage({
      key: 'isDeliverable',
      success: res => {
        if (res.data == "0") {
          this.setData({
            isDeliverable: true
          })
        } else {
          this.setData({
            isDeliverable: false
          })
        }
      },
    })
    this.locateCurAddress();
  },
  onUnload() {
    app.globalData.selectTempCityId = "";
    app.globalData.selectTempCity = "";
    clearTimeout(hideTimer);

  },
  onHide() {
    clearTimeout(hideTimer);
  },
  hideToast() {
    hideTimer = setTimeout(function() {
      wx.hideToast()
    }, 3500)
  },
  locateCurAddress() {
    var gdMap = new amapFile.AMapWX({
      key: common.gdKey
    });
    gdMap.getRegeo({
      success: res => {
        this.setData({
          location: res[0].regeocodeData.pois[0].name,
          curCity: app.globalData.selectTempCity == "" ? res[0].regeocodeData.addressComponent.city : app.globalData.selectTempCity
        })
        app.globalData.locateCity = res[0].regeocodeData.addressComponent.city;
        this.data.curPname = res[0].regeocodeData.addressComponent.province;
        this.data.curAdname = res[0].regeocodeData.addressComponent.district;
        this.data.curCityname = res[0].regeocodeData.addressComponent.city;
        this.data.curla = res[0].latitude;
        this.data.curlon = res[0].longitude;
        server.postJSON(common.storePath + 'address/getcityid/', {
          "city": this.data.curCityname
        }, res => {
          if (res.data.errno == "0") {
            this.data.curCityid = res.data.data;
          } else {
            common.showErrorToast(res.data.errmsg);
          }
        })
        common.getPlaceAround(res[0].longitude, res[0].latitude).then(res => {
          this.setData({
            nearbyBuildings: res.data
          })
        })
      },
      fail: info => {
        common.showErrorToast('定位中...')
      }

    })
  },
  searchAddress(e) {
    this.setData({
      searchValue: e.detail.value
    })
    if (e.detail.value != " " && e.detail.value.length != 0) {
      common.searchPlace(e.detail.value, this.data.curCity).then(res => {
        this.setData({
          resultBuildings: res.data,
          showNearBy: false
        })
      })
    }
  },
  selectAddr(e) {
    var curtarget = e.currentTarget.dataset;
    app.globalData.editAddrLocation = curtarget.name;
    app.globalData.editAddrLatitude = curtarget.la;
    app.globalData.editAddrLongitude = curtarget.lon;
    app.globalData.editAddrCity = curtarget.city;
    app.globalData.editAddrCityId = curtarget.cityid;
    app.globalData.editAddrPname = curtarget.pname; //地址编辑选择省
    app.globalData.editAddrCityname = curtarget.cityname; //地址编辑选择城市
    app.globalData.editAddrAdname = curtarget.adname; //地址编辑选择区
    wx.navigateBack();
    app.globalData.selectTempCityId = "";
    app.globalData.selectTempCity = "";
  },
  selectSearchAddr(e) {
    var curtarget = e.currentTarget.dataset;
    app.globalData.editAddrLocation = curtarget.name;
    app.globalData.editAddrLatitude = curtarget.la;
    app.globalData.editAddrLongitude = curtarget.lon;
    app.globalData.editAddrCity = app.globalData.selectTempCity == "" ? this.data.curCityname : app.globalData.selectTempCity;
    app.globalData.editAddrCityId = app.globalData.selectTempCityId == "" ? this.data.curCityid : app.globalData.selectTempCityId;
    app.globalData.editAddrPname = curtarget.pname; //地址编辑选择省
    app.globalData.editAddrCityname = curtarget.cityname; //地址编辑选择城市
    app.globalData.editAddrAdname = curtarget.adname; //地址编辑选择区
    wx.navigateBack();
    app.globalData.selectTempCityId = "";
    app.globalData.selectTempCity = "";
  },
  chooseCur(e) {
    var curtarget = e.currentTarget.dataset;
    app.globalData.editAddrLocation = curtarget.cur;
    app.globalData.editAddrLatitude = this.data.curla;
    app.globalData.editAddrLongitude = this.data.curlon;
    app.globalData.editAddrCity = this.data.curCityname;
    app.globalData.editAddrPname = this.data.curPname; //地址编辑选择省
    app.globalData.editAddrCityname = this.data.curCityname; //地址编辑选择城市
    app.globalData.editAddrAdname = this.data.curAdname; //地址编辑选择区
    app.globalData.editAddrCityId = this.data.curCityid;
    wx.navigateBack();
    app.globalData.selectTempCity = "";
    app.globalData.selectTempCityId="";
  },
  clearInput() {
    this.setData({
      searchValue: "",
      showNearBy: true
    })
  },
  showCityList() {
    wx.navigateTo({
      url: '../../pages/common/cityList?fromtype=editaddr',
    })
  }

})